using System.Windows;
using System.Windows.Media;
using System.Windows.Forms;

namespace AudioTransfer.Views
{
    /// <summary>
    /// ColorDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ColorDialog : Window
    {
        public Color SelectedColor { get; private set; } = Colors.LightBlue;
        public Color GradientColor { get; private set; } = Colors.LightBlue;
        public double Alpha { get; private set; } = 255;
        public bool IsGradient { get; private set; } = false;

        public ColorDialog()
        {
            InitializeComponent();
            this.Loaded += ColorDialog_Loaded;
        }

        private void ColorDialog_Loaded(object sender, RoutedEventArgs e)
        {
            // 初始化默认预览
            UpdatePreview();
        }

        private void SelectColorButton_Click(object sender, RoutedEventArgs e)
        {
            using (var colorDialog = new System.Windows.Forms.ColorDialog())
            {
                // 设置默认颜色为 LightBlue
                colorDialog.Color = System.Drawing.Color.FromArgb(173, 216, 230);
                colorDialog.AllowFullOpen = true;
                colorDialog.FullOpen = true;
                
                if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var selectedColor = colorDialog.Color;
                    
                    // 转换为 WPF Color
                    SelectedColor = Color.FromRgb(selectedColor.R, selectedColor.G, selectedColor.B);
                    GradientColor = Color.FromArgb(255, selectedColor.R, selectedColor.G, selectedColor.B);
                    
                    // 更新预览
                    UpdatePreview();
                    
                    // 更新颜色信息显示
                    ColorInfo.Text = $"RGB({selectedColor.R}, {selectedColor.G}, {selectedColor.B})";
                    HexInfo.Text = $"#{selectedColor.R:X2}{selectedColor.G:X2}{selectedColor.B:X2}";
                }
            }
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        // 颜色类型切换事件
        private void ColorType_Changed(object sender, RoutedEventArgs e)
        {
            if (GradientColorRadio != null)
            {
                IsGradient = GradientColorRadio.IsChecked == true;
                UpdatePreview();
            }
        }

        // 更新预览区域
        private void UpdatePreview()
        {
            if (ColorPreview == null || GradientPreview == null) return;

            if (IsGradient)
            {
                // 渐变色预览
                ColorPreview.Fill = new SolidColorBrush(Colors.Transparent);
                GradientPreview.Fill = new LinearGradientBrush(
                    new GradientStopCollection
                    {
                        new GradientStop(Colors.White, 0.0),
                        new GradientStop(SelectedColor, 1.0)
                    },
                    new System.Windows.Point(0, 0),
                    new System.Windows.Point(1, 1)
                );
            }
            else
            {
                // 纯色预览
                ColorPreview.Fill = new SolidColorBrush(SelectedColor);
                GradientPreview.Fill = new SolidColorBrush(SelectedColor);
            }
        }
    }
}
